package com.liang.blog.service.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import org.springframework.util.StringUtils;

/**
 * @Author：YanWenLiang
 * @Date:2023-08-24-21:43
 */
public class TokenUtil {
    private static String tokenSignKey = "qwertyuiopasdfghjklzxcvbnm";

    public static Claims getClaims(String token){
        if(StringUtils.isEmpty(token)) return null;
        Jws<Claims> claimsJws = Jwts.parser().setSigningKey(tokenSignKey).parseClaimsJws(token);
        return claimsJws.getBody();
    }

    public static Long getUserId(String token) {
        Integer userId = (Integer) getClaims(token).get("userId");
        return userId.longValue();
    }

    public static String getAccount(String token) {
        return (String) getClaims(token).get("account");
    }

    public static String getUserKey(String token){
        return (String) getClaims(token).get("userKey");
    }

}
